//
//  HWMineNoticeCell.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/29.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import FSPagerView
class HWMineNoticeCell: UICollectionViewCell {
    var imageView:UIImageView!
    var titleBanner:FSPagerView!
    var models:[HWMineNewsModel]? {
        didSet {
            titleBanner.reloadData()
        }
    }
    
    var didClickNotice:((_:HWMineNewsModel)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
extension HWMineNoticeCell {
    private func initUI() -> Void {
        imageView = UIImageView()
        imageView.image = UIImage(named: "hornPromote")
        contentView.addSubview(imageView)
        
        titleBanner = FSPagerView()
        titleBanner.dataSource = self
        titleBanner.delegate = self
        titleBanner.isInfinite = true
        titleBanner.register(HWNoticeCell.self, forCellWithReuseIdentifier: "HWNoticeCell")
        titleBanner.automaticSlidingInterval = 3
        titleBanner.removesInfiniteLoopForSingleItem = true
        titleBanner.scrollDirection = .vertical
    
        contentView.addSubview(titleBanner)
    }
    
    private func initFrame() -> Void {
        imageView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(10*widthScale)
        }
        
        titleBanner.snp.makeConstraints { make in
            make.left.equalTo(imageView.snp.right).offset(4*widthScale)
            make.right.equalTo(-10*widthScale)
            make.height.equalTo(26*widthScale)
            make.centerY.equalToSuperview()
        }
    }
}

extension HWMineNoticeCell:FSPagerViewDelegate,FSPagerViewDataSource {
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return models?.count ?? 0
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "HWNoticeCell", at: index) as! HWNoticeCell
        cell.titleLabel.text = models?[index].title
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        didClickNotice?(models?[index] ?? HWMineNewsModel())
    }
    
}

class HWNoticeCell:FSPagerViewCell {
    var titleLabel:UILabel!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func initUI() -> Void {
        titleLabel = HWLabel.createLabel(title: "", color: "#5879FF", font: 10*widthScale)
        addSubview(titleLabel)
    }
    
    private func initFrame() -> Void {
        titleLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}
